package com.qf.xiaom.dao.impl;

import com.qf.xiaom.dao.GoodsDao;
import com.qf.xiaom.pojo.PageBean;
import com.qf.xiaom.pojo.TbGoods;
import com.qf.xiaom.service.GoodsService;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsService {

    @Override
    public List<TbGoods> findByType(Long typeId, PageBean pageBean) {
        //获取到连接
        QueryRunner runner= DbUtils.getRunner();
        //计算limit的偏移量
        int pyl=(pageBean.getCurrenPage()-1)*pageBean.getPageSize();
        //我们要执行的sql
        String sql="SELECT tg.*, tgz.name,tgz.pubdate,tgz.intro,tgz.picture,tgz.price,tgz.star,tgz.typeid  from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=? limit?,?";
        //查询总条数的sql
        String countSql="select COUNT(0) totalSize from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=?";
        List<TbGoods>goodsList=null;
        try{
            //指定sql获取到返回的结果
            goodsList=runner.query(sql,new BeanHandler<>(TbGoods.class),typeId,pyl,pageBean.getPageSize());
            //获取到当前分类的商品的数量
            Map<String,Object>resultMap=runner.query(countSql,new MapHandler(),typeId);
            //设置当前查询到的集合数据
            pageBean.setTotalSize(Long)re;

        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TbGoods findById(Long id) {
        return null;
    }

    @Override
    public List<TbGoods> findByIds(java.awt.List ids) {
        return null;
    }
}
